package myPetStore.service;

import myPetStore.domain.EmailObject;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class EmailService extends Thread{

    private Properties props = new Properties();
    private EmailObject mailObj=new EmailObject();
    private String code;
    private String email;
    public EmailService(String code,String email){
        this.email=email;
        this.code=code;
    }
    public void sendEmail(String code,String email) throws MessagingException {
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", 465);
        props.put("mail.smtp.ssl", "true");
        props.put("mail.smtp.ssl.enable", "true");

        // 需要认证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.user", "283999702@qq.com");
        props.put("mail.smtp.pass", "fznnrbgcjlhgcaff");
        // 使用ssl
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailObj.setFrom("283999702@qq.com");
        mailObj.setTo(email);
        mailObj.setSubject("注册验证码：");
        mailObj.setFormat(EmailObject.EmailFormat.TEXT);
        mailObj.setBody(code);

        //创建session
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (Boolean.parseBoolean(props.getProperty("mail.smtp.auth"))) {
                    // 需要认证
                    return new PasswordAuthentication(props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));
                }
                return super.getPasswordAuthentication();
            }
        });
        Transport ts=session.getTransport();
        ts.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));

        MimeMessage msg = new MimeMessage(session);

        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        msg.setSubject(mailObj.getSubject());
        msg.setFrom(new InternetAddress(mailObj.getFrom()));
        msg.setText(mailObj.getBody());
        msg.setSentDate(new Date());


        ts.sendMessage(msg, msg.getAllRecipients());


    }
    @Override
    public void run(){
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", 465);
        props.put("mail.smtp.ssl", "true");
        props.put("mail.smtp.ssl.enable", "true");

        // 需要认证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.user", "283999702@qq.com");
        props.put("mail.smtp.pass", "fznnrbgcjlhgcaff");
        // 使用ssl
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailObj.setFrom("283999702@qq.com");
        mailObj.setTo(email);
        mailObj.setSubject("注册验证码：");
        mailObj.setFormat(EmailObject.EmailFormat.TEXT);
        mailObj.setBody(code);

        //创建session
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (Boolean.parseBoolean(props.getProperty("mail.smtp.auth"))) {
                    // 需要认证
                    return new PasswordAuthentication(props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));
                }
                return super.getPasswordAuthentication();
            }
        });
        try {
            Transport ts= session.getTransport();;
            ts = session.getTransport();
            ts.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.smtp.user"), props.getProperty("mail.smtp.pass"));
            MimeMessage msg = new MimeMessage(session);

            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            msg.setSubject(mailObj.getSubject());
            msg.setFrom(new InternetAddress(mailObj.getFrom()));
            msg.setText(mailObj.getBody());
            msg.setSentDate(new Date());


            ts.sendMessage(msg, msg.getAllRecipients());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
